package servlet;

import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/delete")
public class deleteUserServlet extends HttpServlet {
    //@WebServlet与页面链接
    //更新用户，保存提交

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //编码处理  处理汉字传输乱码问题
        req.setCharacterEncoding("utf-8");
        //获取页面隐藏的id
        String sid = req.getParameter("id");
        Integer id=Integer.parseInt(sid);//转换把sid字符串类型转为int类型
        //拿到页面传递的参数
        UserService userService = new UserServiceImpl();
        int result=userService.deleteUserById(id);
        if(result>0){
            //重定向 再次查询 新增的数据能表示出来
            resp.sendRedirect("user");
        }
    }
}
